package pers.hl.module_main.admin.lib

import pers.hl.common.base.BaseViewModel
import pers.hl.common.base.SingleLiveEvent
import pers.hl.lib_global.model.bean.Library

class LibManageViewModel: BaseViewModel<LibManageModel>() {

    private val addLibEvent = SingleLiveEvent<Library>()
    private val getLibEvent = SingleLiveEvent<List<Library>>()
    private val deleteLibEvent = SingleLiveEvent<String>()
    private val searchLibEvent = SingleLiveEvent<List<Library>>()

    fun addLibrary(library: Library): SingleLiveEvent<Library> {
        launchOnIO {
            val result = mModel.addLibrary(library)
            val code = result.code
            val msg = result.msg
            addLibEvent.postValue(result.data)
        }
        return addLibEvent
    }

    fun getAllLibraries(): SingleLiveEvent<List<Library>> {
        launchOnIO {
            val result = mModel.getAllLibraries()
            getLibEvent.postValue(result.data)
        }
        return getLibEvent
    }

    fun deleteLibrary(libId: Int): SingleLiveEvent<String> {
        launchOnIO {
            val result = mModel.deleteLibrary(listOf(libId))
            val code = result.code
            val msg = result.msg
            deleteLibEvent.postValue("删除成功")
        }
        return deleteLibEvent
    }

    fun searchLibrary(keyword: String): SingleLiveEvent<List<Library>> {
        launchOnIO {
            val result = mModel.searchLibrary(keyword)
            searchLibEvent.postValue(result.data)
        }
        return searchLibEvent
    }

}
